# -*- coding: utf-8 -*-
"""
s = "Mas que belo dia"

x = 'q'

if x in s: print " a letra 'q' encontra-se em s"

t = " em Amsterdao"

print s + t

print 3 * t

print s[5], s[5:8], s[5:12:2]

print len(t), len(s)

print min(s), max(t)

print "indice de 'q' =", s.index('q')

print " vezes em que aparece 'm'=" , t.count('m')


#Exemplos
 
s1 = "Anda um espectro pela Europa"
 
i = s1.find('ctr0', 3, 20)

print s1[i:i+5]
 
print s1.strip('a')
 
t = s1.partition(' ')

print t

l = """
"""#comentado
Alma minha gentil, que te partiste

Alma minha gentil, que te partiste
Tao cedo desta vida, descontente
Repousa la no Ceu eternamente
E viva eu ca na terra sempre triste.
"""# comentado
""".splitlines()
print l


import math

x = 23.4

s1 = "raiz quadrada"

y = 255

s2 = "Temos {:^40} de x = {valor:2.4f}".format(s1,
      valor=x)
   
s2 += "e y = 0x{yy:X}".format(yy=y)
s2 += "\nA percentagem = {0:2.1%}".format(34/100.0)
print s2


# Leitura de ficheiros

f = open("teste.txt", "w")

s = "treta"

for K in range(10): f.write(s); f.write("\n")

f.close()

f = open("teste.txt", "r")

f.seek(5)

print f.tell()

x = f.read(10)

print x

c = 0

for line in f:
	print "{0}:".format(c), line
	c +=1
f.close()

with open("teste.txt", "r") as f:
	x = f.readline()
	if x =='treta\n': print "voila"
# classe pickle
import pickle
class Teste:
	def __init__(self, x):
		self.x = x
obj = Teste(49)
f = open("teste.dat","w")
p = pickle.dump(obj, f)
f.close
del obj
obj = pickle.load(open("teste.dat", "r"))
print obj.x


# Ficheiros CSV

import csv

with open('listacompras.csv', 'rb') as csvfile:
	leitor =csv.reader(csvfile)
	for linha in leitor:
		print ",".join(linha)
		
with open('notas.csv', 'wb') as csvfile:
	escritor = csv.write(csvfile)
	esritor.writerow(['Programacao 1', 14])
	escritor.writerow(['Programacao 2', 15])
	escritor.writerow(['Linguagens de Programacao', 19])


# Base dados sqlite3

import sqlite3

conexao = sqlite3.connect('teste.db')
c = conexao.cursor()
c.execute('DROP TABLE IF EXISTS disc')
c.execute('CREATE TABLE disc VALUES (?,?)', notas)
conexao.commit()

notas = [('P1', 15), ('P2',15), ('LP',19)]
c.executemany('INSERT INTO disc VALUES(?,?)', nota)
conexao.commit()
c.execute("SELECT * FROM disc WHERE nota > 16"):
print "uma...", c.fetchone()
K = 0; soma = 0.0
for linha in c.execute("SELECT * FROM disc"):
	print linha; soma += linha [1]; K += 1
print "media = {:2.2f}".format(soma/K)
conexao.close

"""
# excepçoes

import sys

try:
	f = open('ficha.text')
	s = f.readline()
	i = int(s.strip())
except IOError as e:
	print"I/O error({0}): {1}".format(e.errno, e.strerror)
except ValueError:
	print" Could not convert data to an integer."
except:
	print " Unexpected error:", sys.exec_info()[0]
	raise
